/**
 * GULP Required components
 */
var gulp = require('gulp');
var del = require('del');
var header = require('gulp-header');
var footer = require('gulp-footer');
var concat = require('gulp-concat');
var sass = require('gulp-sass');
var minifyCss = require('gulp-minify-css');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var inject = require('gulp-inject');


/**
 * Project global Settings
 * @type {string}
 */

//bower path
var bower_components_path = './bower_components/**';
//library path
var lib_path = './www/lib';
//web root path
var index_path = './www/index.html';
// css lib for inject
var cssInjectLib = [
    './www/lib/angular-bootstrap-contextmenu/bootstrap.min.css',
    './www/dest/font/iconfont.css',
    './www/lib/malihu-custom-scrollbar-plugin/jquery.mCustomScrollbar.min.css'
];
// js lib for inject
var jsInjectLib = [
    './www/lib/jQuery/dist/jquery.js',
    './www/lib/angular/angular.js',
    './www/lib/jquery-ui/jquery-ui.min.js',
    './www/lib/oclazyload/dist/ocLazyLoad.js',
    './www/lib/jquery-mousewheel/jquery.mousewheel.min.js',
    './www/lib/malihu-custom-scrollbar-plugin/jquery.mCustomScrollbar.js',
    './www/lib/angular-ui-sortable/sortable.min.js',
    './www/lib/velocity/velocity.min.js',
    './www/lib/angular-bootstrap-contextmenu/contextMenu.js'
];

gulp.task('makeDependencies',function(){
    gulp.src(bower_components_path)
        .pipe( gulp.dest(lib_path) )
});

gulp.task('inject', function(){

    gulp.src( index_path )
        .pipe(
            inject(
                gulp.src(jsInjectLib, {
                    reade : false
                }),
                { relative : true }
            )
        )
        .pipe( gulp.dest('./www/') )
        .pipe(
            inject(
                gulp.src(cssInjectLib, {
                    read : false
                }),
                { relative : true }
            )
        )
        .pipe( gulp.dest('./www/') );
});

/**
 * plugin multiCTC Gulp Part
 * @type {string}
 */
var p_sass_all = './www/src/sass/*.scss';
var p_sass_path = './www/src/sass/zhx.multi.ctc.scss';
var p_js_path = './www/src/js/*.js';
var p_dest_path = './www/dest/';

gulp.task('publish',function(){

    gulp.src(p_sass_path)
        .pipe( sass() )
        .pipe( gulp.dest( p_dest_path) )
        .pipe( minifyCss() )
        .pipe( rename({ extname : '.min.css' }) )
        .pipe( gulp.dest( p_dest_path ) );

    gulp.src(p_js_path)
        .pipe( header(' (function(){\n "use strict";\n ' ) )
        .pipe( footer(' \n})(); ') )
        .pipe( gulp.dest( p_dest_path) )
        .pipe( uglify() )
        .pipe( rename({ extname : '.min.js' }) )
        .pipe( gulp.dest( p_dest_path ) );
    //
    // gulp.src(index_path)
    //     .pipe( inject( gulp.src('./www/dest/*.css', {read : false}), {
    //         relative : true,
    //         name : 'ctc'
    //     }))
    //     .pipe( gulp.dest('./www/') );
    //
    // gulp.src(index_path)
    //     .pipe( inject( gulp.src('./www/dest/*.js',{read: false}), {
    //         relative : true,
    //         name : 'ctc'
    //     }))
    //     .pipe( gulp.dest('./www/') )

});

gulp.task('watch',function(){
    gulp.watch([p_sass_all,p_js_path], ['publish']);
});



